﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ConstellationShow.Function.DocData;
using System.IO;
using System.Xml.Linq;
using System.Windows.Media.Imaging;

namespace ConstellationShow.Pages
{
    public partial class Famous : MyPage
    {
        string id;
        string sid;
        public Famous()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Famous_Loaded);
        }

        void Famous_Loaded(object sender, RoutedEventArgs e)
        {
            id = NavigationContext.QueryString["id"];
            sid = NavigationContext.QueryString["sid"];
            PageTitle.Text = Info.ConvStr(id);
            _progress.IsVisible = true;
            GetData(Mark.MarkUrl(Url.GetOneFamous) + "&starid=" + sid);
        }

        //拉取信息
        void GetData(string _url)
        {
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += (_, __) =>
            {
                if (__.Error != null)
                {
                    MessageBox.Show("网络错误，请检查网络是否连接好！");
                    _progress.IsVisible = false; return;
                };
                using (Stream streamResult = __.Result)
                {
                    XElement root = XElement.Load(streamResult);
                    var tests = from el in root.Elements("resultinfo") select el;
                    foreach (var a in tests)
                    {
                        if (a.Attribute("result").Value == "1") //成功
                        {
                            var starinfo = from el in root.Elements("starinfo") select el;
                            foreach (var star in starinfo)
                            {
                                txt1.Text = star.Element("starname").Value;
                                txt2.Text = (star.Element("gender").Value == "1") ? "男" : "女";
                                txt3.Text = star.Element("oldname").Value;
                                txt4.Text = star.Element("ename").Value;
                                txt5.Text = star.Element("year").Value;
                                txt6.Text = star.Element("month").Value + "月" + star.Element("day").Value + "日";
                                txt7.Text = star.Element("constellation").Value;
                                txt8.Text = star.Element("nationlity").Value;
                                txt9.Text = star.Element("zone").Value;
                                txt10.Text = star.Element("stature").Value;
                                txt11.Text = star.Element("rank").Value;
                                if (star.Element("picurl").Value!="")
                                    img.Source = new BitmapImage(new Uri(star.Element("picurl").Value, UriKind.Absolute));
                                num1.Text = star.Element("spell").Value;
                                num2.Text = star.Element("strength").Value;
                                text.Text = star.Element("intro").Value.Replace("<br/>", "\n");
                                _progress.IsVisible = false;
                            }
                        }
                    }
                }
            };
            webClient.OpenReadAsync(new Uri(_url), webClient);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int tag = Convert.ToInt32((sender as Button).Tag.ToString());
            object _vote = Com.GetSetting("vote_" + sid);
            if (_vote != null && _vote.ToString() == DateTime.Now.ToShortDateString().ToString())
            {
                MessageBox.Show("你今天已经投过票了，请明天再来吧！");
                return;
            }
            _progress.IsVisible = true;
            string _url = Mark.MarkUrl(Url.FamousVote) + "&starid=" + sid + "&order=" + tag;
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += (_, __) =>
            {
                if (__.Error != null)
                {
                    MessageBox.Show("网络错误，请检查网络是否连接好！");
                    _progress.IsVisible = false; return;
                };
                using (Stream streamResult = __.Result)
                {
                    XElement root = XElement.Load(streamResult);
                    var tests = from el in root.Elements("resultinfo") select el;
                    foreach (var a in tests)
                    {
                        if (a.Attribute("result").Value == "1") //成功
                        {
                            Com.SetSetting("vote_" + sid, DateTime.Now.ToShortDateString().ToString());
                            MessageBox.Show("投票成功！");
                            if (tag == 0)
                                num1.Text = (Convert.ToInt32(num1.Text) + 1).ToString();
                            else
                                num2.Text = (Convert.ToInt32(num2.Text) + 1).ToString();
                        }
                        else
                        {
                            MessageBox.Show(a.Attribute("resultmsg").Value);
                        }
                        _progress.IsVisible = false;
                    }
                }
            };
            webClient.OpenReadAsync(new Uri(_url), webClient);
        }

        private void Click_Share(object sender, EventArgs e)
        {
            Com.Share(Info.ConvStr(id.ToString()) + "的" + txt1.Text + "资料", text.Text);
        }
    }
}